package com.miniso.robot.bean;

import cn.hutool.extra.spring.SpringUtil;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.miniso.robot.feign.RobotFeign;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guojunwang
 * @date 2021-10-19 9:08
 */
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@Getter
@NoArgsConstructor
public class TextCardMessage extends Message{

    private final String msgtype = "template_card";

    private Map<String,Object> templateCard = new HashMap(8);


    {
        templateCard.put( "card_type","text_notice" );
        templateCard.put( "horizontal_content_list",new ArrayList());
        templateCard.put( "jump_list",new ArrayList());
    }

    public void setSource(String iconUrl,String desc){
        Map<String,Object> source = new HashMap(8);
        source.put("icon_url",iconUrl);
        source.put("desc",desc);
        templateCard.put("source",source);
    }

    public void setMainTitle(String title,String desc){
        Map<String,Object> titleMap = new HashMap(8);
        titleMap.put("title",title);
        titleMap.put("desc",desc);
        templateCard.put("main_title",titleMap);
    }

    public void setDataContent(String number,String desc){
        Map<String,Object> content = new HashMap(8);
        content.put("title",number);
        content.put("desc",desc);
        templateCard.put("emphasis_content",content);
    }

    public void setSubTitle(String subTitle){
        templateCard.put("sub_title_text",subTitle);
    }

    /**
     * 新增文字
     * @param keyname 前缀
     * @param value 文字内容
     */
    public void addHorizontalText(String keyname,String value){
        List list = (ArrayList)templateCard.get("horizontal_content_list");
        Map<String,String> content = new HashMap(8);
        content.put("keyname",keyname);
        content.put("value",value);
        list.add(content);
    }

    /**
     * 新增附件
     * @param keyname 前缀
     * @param value 文件名称
     * @param mediaId {@link com.miniso.robot.feign.RobotFeign#uploadFile(String, MultipartFile)}
     */
    public void addHorizontalFile(String keyname,String value,String mediaId ){

        Map<String, Object> content = new HashMap(8);
        content.put("keyname",keyname);
        content.put("value",value);
        content.put("type",2);
        content.put("media_id",mediaId);

        List list = (ArrayList)templateCard.get("horizontal_content_list");
        list.add(content);
    }

    /**
     * 新增跳转连接
     * @param keyname 前缀
     * @param value 文件名称
     * @param url 点击跳转的url
     */
    public void addHorizontalRedirect(String keyname,String value,String url ){
        Map<String, Object> content = new HashMap(8);
        content.put("keyname",keyname);
        content.put("value",value);
        content.put("type",1);
        content.put("url",url);

        List list = (ArrayList)templateCard.get("horizontal_content_list");
        list.add(content);
    }

    /**
     * 新增跳转url
     * @param title
     * @param url
     */
    public void addUrlJump(String title,String url){
        List list = (ArrayList)templateCard.get( "jump_list");

        Map map = new HashMap(8);
        map.put("type",1);
        map.put("title",title);
        map.put("url",url);

        list.add(map);
    }

    /**
     * 新增跳转app
     * @param title
     * @param appId
     * @param pagePath
     */
    public void addAppJump(String title,String appId,String pagePath){
        List list = (ArrayList)templateCard.get( "jump_list");

        Map map = new HashMap(8);
        map.put("title",title);
        map.put("type",2);
        map.put("appid",appId);
        map.put("pagepath",pagePath);
        list.add(map);
    }

    /**
     * 设置整个卡片跳转的url
     * @param url
     */
    public void setUrlJumpCardAction(String url){
        Map map = new HashMap(8);
        map.put("type",1);
        map.put("url",url);
        templateCard.put("card_action",map);
    }

    /**
     * 设置整个卡片跳转的app路径
     * @param appId
     * @param pagePath
     */
    public void setAppJumpCardAction(String appId,String pagePath){
        Map map = new HashMap(8);
        map.put("type",2);
        map.put("appid",appId);
        map.put("pagepath",pagePath);
        templateCard.put("card_action",map);
    }

}
